/**
 * Copyright (C) 2008 GeoScheduler Team, as stated on <http://www.geoscheduler.org/authors>.
 * 
 * This file is part of GeoScheduler.
 * 
 * GeoScheduler is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * GeoScheduler is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public License
 * along with GeoScheduler. If not, see <http://www.gnu.org/licenses/>.
 */

package org.geoscheduler.commons.marshaller.decls;

import java.util.Date;

import org.geoscheduler.commons.marshaller.JSONFieldDecl;
import org.geoscheduler.commons.marshaller.JSONMarshaller;
import org.geoscheduler.commons.tools.PropertyDescriptor;
import org.geoscheduler.commons.tools.StringTools;

/**
 * 
 * <h2>JSONDateField</h2>
 * <p>
 * {@link Date} property marshaller/unmarshaller
 * </p>
 * 
 * @author lko
 */
public class JSONDateField extends JSONAbstractSimpleFieldDecl<Date> {

	public JSONDateField(PropertyDescriptor<Date> desc, Date defValue) {
		super(desc, defValue);
	}

	public JSONDateField(PropertyDescriptor<Date> desc) {
		super(desc);
	}

	/**
	 * @see org.geoscheduler.commons.marshaller.decls.JSONAbstractSimpleFieldDecl#decString(java.lang.String)
	 */
	public Date decString(String value) {
		return StringTools.isEmptyTrimmed(value) ? getDefaultValue() : new Date(Long.parseLong(value));
	}

	/**
	 * @see org.geoscheduler.commons.marshaller.decls.JSONAbstractSimpleFieldDecl#encString(java.lang.Object)
	 */
	public String encString(Date value) {
		return value == null ? null : String.valueOf(value.getTime());
	}

	public static class Factory extends BaseAbstractFactory<Date> {

		public JSONFieldDecl<Date> create(PropertyDescriptor<Date> desc, JSONMarshaller marshaller) {
			return new JSONDateField(desc);
		}

		@Override
		public boolean isApplicable(PropertyDescriptor<?> desc) {
			return super.isApplicable(desc) && Date.class.isAssignableFrom(desc.getType());
		}
	}

}
